package net.qiqbframework.serialization.json;


import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import net.qiqbframework.serialization.ContentTypeConverter;
import net.qiqbframework.serialization.SerializationException;

import com.fasterxml.jackson.databind.node.ObjectNode;


public class JsonNodeToObjectNodeConverter implements ContentTypeConverter<JsonNode, ObjectNode> {

    @Override
    public Class<JsonNode> expectedSourceType() {
        return JsonNode.class;
    }

    @Override
    public Class<ObjectNode> targetType() {
        return ObjectNode.class;
    }

    @Override
    public ObjectNode convert(JsonNode original) {
        JsonNodeType originalNodeType = original.getNodeType();
        if (JsonNodeType.OBJECT.equals(originalNodeType)) {
            return ((ObjectNode) original);
        } else {
            throw new SerializationException(
                    "Cannot convert from JsonNode to ObjectNode because the node type is [" + originalNodeType + "]"
            );
        }
    }
}
